package com.shangguan.realm;

import com.shangguan.beans.TPermissions;
import com.shangguan.beans.TRole;
import com.shangguan.beans.TUser;
import com.shangguan.mapper.TPermissionsMapper;
import com.shangguan.mapper.TRoleMapper;
import com.shangguan.mapper.TUserMapper;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName: ShiroRealm
 * @Description:
 * @Author: 一尘
 * @Date: 2020 年 03 月 19 20:02
 * @Version 1.0
 */
/*作认证和授权的
* 在这里面我们进行输入的数据和数据库的数据进行对比
* */
public class ShiroRealm extends AuthorizingRealm {
    @Autowired
    TUserMapper  tUserMapper;
    @Autowired
    TRoleMapper tRoleMapper;
    @Autowired
    TPermissionsMapper tPermissionsMapper;
    Logger  logger = LoggerFactory.getLogger(ShiroRealm.class)   ;
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.info("---------------开始授权---------------");
        System.out.println(principals.getPrimaryPrincipal());
        String  userName  = (String)principals.getPrimaryPrincipal();
        //获取当前用户的角色
        List<TRole> roles = tRoleMapper.selectRolesByUserName(userName);
        SimpleAuthorizationInfo  simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        for (TRole role : roles) {
                if(role==null){
                    return  null;
                }
                //添加角色验证
            simpleAuthorizationInfo.addRole(role.getRoleName());
           //获取角色的权限
      List<TPermissions>  permissionsList =    tPermissionsMapper.selectPermissionsByRoleName(role.getRoleName());
            for (TPermissions tPermissions : permissionsList) {
                     if(tPermissions==null){
                         return  null;
                     }
                     //添加权限验证
                simpleAuthorizationInfo.addStringPermission(tPermissions.getPermissionsName());
            }
        }
        return simpleAuthorizationInfo;
    }
     //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
            throws AuthenticationException {
        logger.info("-------------开始认证----------------");
        //获取用户输入的用户名
        String  userName =(String) token.getPrincipal();
        //根据用户名查询用户信息
           TUser  tUser =  tUserMapper.selectByUserName(userName);
           logger.info("---------"+tUser+"-----------"+userName);
           if(tUser==null){
               return null;
           }
        //盐
        ByteSource byteSource =    ByteSource.Util.bytes(userName);
           //密码加密      加密的算法  数据(密码) 盐 加密次数
        SimpleHash  hash = new SimpleHash("SHA1",tUser.getUserPassword(),byteSource,1024);

         //接下来我们要去进行这个信息的比较
        SimpleAuthenticationInfo simpleAuthenticationInfo =  //用户名  密码  当前的realm
                //new SimpleAuthenticationInfo(userName,tUser.getUserPassword(),this.getName());
                //加密的密码验证
                new SimpleAuthenticationInfo(userName,hash,byteSource,this.getName());
        return simpleAuthenticationInfo;
    }
}
